//
//  TTCoreCommon.m
//  Tint
//
//  Created by Kevin Wu on 8/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTCoreCommon.h"


#pragma mark - Compatibility

BOOL TTIsPad()
{
    return ( [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad );
}

BOOL TTIsPod()
{
    return [[UIDevice currentDevice].model hasPrefix:@"iPod"];
}

BOOL TTIsPhone()
{
    return [[UIDevice currentDevice].model hasPrefix:@"iPhone"];
}

CGFloat TTOSVersion()
{
    return [[[[UIDevice class] currentDevice] systemVersion] floatValue];
}



#pragma mark - Launch times

BOOL TTIsFirstLaunch()
{
    return ( TTLaunchTimes() == 1 );
}

NSUInteger TTLaunchTimes()
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"TTLaunchTimesKey"];
}

void TTIncreaseLaunchTimes()
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSUInteger times = [userDefaults integerForKey:@"TTLaunchTimesKey"];
    [userDefaults setInteger:(times + 1) forKey:@"TTLaunchTimesKey"];
    [userDefaults synchronize];
}



#pragma mark - Paths

NSString *TTDocumentsPath()
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    if ( [paths count] > 0 ) {
        return [paths objectAtIndex:0];
    }
    return nil;
}

NSString *TTLibraryPath()
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    if ( [paths count] > 0 ) {
        return [paths objectAtIndex:0];
    }
    return nil;
}

NSString *TTCachesPath()
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    if ( [paths count] > 0 ) {
        return [paths objectAtIndex:0];
    }
    return nil;
}

NSString *TTBundlePath()
{
    return [[NSBundle mainBundle] bundlePath];
}



#pragma mark - Image cache

UIImage *TTCachedImage(NSString *name)
{
    return [UIImage imageNamed:name]; // AUTO
}

UIImage *TTUncachedImage(NSString *name)
{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    return [UIImage imageWithContentsOfFile:path]; // AUTO
}

UIImage *TTCreateUncachedImage(NSString *name)
{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    return [[UIImage alloc] initWithContentsOfFile:path];
}


#pragma mark - Validity

BOOL TTIsInstance(id object, Class cls)
{
    return ((object) && 
            [object isKindOfClass:cls]
            );
}

BOOL TTIsStringWithText(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSString class]] && 
            ([(NSString *)object length] > 0)
            );
}

BOOL TTIsDataWithBytes(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSData class]] && 
            ([(NSData *)object length] > 0)
            );
}

BOOL TTIsArrayWithItems(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSArray class]] && 
            ([(NSArray *)object count] > 0)
            );
}

BOOL TTIsDictionaryWithItems(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSDictionary class]] && 
            ([(NSDictionary *)object count] > 0)
            );
}

BOOL TTIsSetWithItems(id object)
{
    return ((object) && 
            [object isKindOfClass:[NSSet class]] && 
            ([(NSSet *)object count] > 0)
            );
}


#pragma mark - RFC 3339 date time string

static NSDateFormatter *sharedRFC3339DateFormatter = nil;

NSDateFormatter *TTRFC3339DateTimeFormatter()
{
    if ( sharedRFC3339DateFormatter == nil ) {
        sharedRFC3339DateFormatter = [[NSDateFormatter alloc] init];
        NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
        
        [sharedRFC3339DateFormatter setLocale:enUSPOSIXLocale];
        [sharedRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
        [sharedRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [enUSPOSIXLocale release];
    }
    return sharedRFC3339DateFormatter;
}

void TTDestroyRFC3339DateTimeFormatter()
{
    [sharedRFC3339DateFormatter release];
    sharedRFC3339DateFormatter = nil;
}

NSDate *TTDateFromRFC3339DateTimeString(NSString *string)
{
    if ( string ) {
        return [TTRFC3339DateTimeFormatter() dateFromString:string]; // AUTO
    }
    return nil;
}

NSString *TTRFC3339DateTimeStringFromDate(NSDate *date)
{
    if ( date ) {
        return [TTRFC3339DateTimeFormatter() stringFromDate:date]; // AUTO
    }
    return nil;
}



#pragma mark - NonRetaining collections

NSMutableArray *TTCreateNonRetainingMutableArray()
{
    return (NSMutableArray *)CFArrayCreateMutable(nil, 0, nil);
}

NSMutableDictionary *TTCreateNonRetainingMutableDictionary()
{
    return (NSMutableDictionary *)CFDictionaryCreateMutable(nil, 0, nil, nil);
}

NSMutableSet *TTCreateNonRetainingMutableSet()
{
    return (NSMutableSet *)CFSetCreateMutable(nil, 0, nil);
}

